/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAOs.SatellitalImageJpaController;
import Entity.SatellitalImage;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import javax.imageio.ImageIO;
import javax.persistence.EntityManagerFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author USUARIO
 */
public class satellitalImageController {
    public SatellitalImage createSatellitalImage(Image image, String discriminator, EntityManagerFactory emf){
        try {
            Calendar fecha = Calendar.getInstance();
            discriminator.replace('.','-');
            String dir = discriminator + fecha.get(Calendar.DAY_OF_MONTH) + "_"
                    + (fecha.get(Calendar.MONTH)+1) + "_" + fecha.get(Calendar.YEAR) + ".png";
            System.out.println("Images\\" + dir);
            File file = new File("Images\\"+ dir);
            SatellitalImage sateImage = new SatellitalImage();
            sateImage.setDir(dir);
            sateImage.setFecha(fecha);
            ImageIO.write((BufferedImage)image, "png", file);
            SatellitalImageJpaController satellitalJPA = new SatellitalImageJpaController(emf);
            satellitalJPA.create(sateImage);
            return sateImage;            
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(new JFrame(), "No se puede guardar la imagen", "Dialog", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    }
}
